﻿using Rbac.Domain.Entity;
using Rbac.Domain.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Rbac.Domain.Impl
{
	/// <summary>
	/// 用户角色仓储实现
	/// </summary>
	public class UserRoleRepository : IUserRoleRepository
	{
		private readonly RbacContext db;
		public UserRoleRepository(RbacContext db)
		{
			this.db = db;
		}
		/// <summary>
		/// 返回所有的用户角色信息
		/// </summary>
		/// <returns></returns>
		public IQueryable<UserRole> UserRoles()
		{
			return db.UserRoles;
		}
		/// <summary>
		/// 根据用户查询角色
		/// </summary>
		/// <param name="userId">用户id</param>
		/// <returns></returns>
		public IQueryable<Role> UserRolesByUserId(params int[] userId)
		{
			//var list = db.UserRoles.Where(c => userId.Contains(c.UserId)).Select(x => x.RoleId);
			//return db.Roles.Where(c=> list.Contains(c.RoleId));
			return from ur in db.UserRoles
				   join r in db.Roles on ur.RoleId equals r.RoleId
				   where userId.Contains(ur.UserId)
				   select r;
		}
	}
}
